package com.iknow.view.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask.Status;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.data.Word;
import com.iknow.http.HttpException;
import com.iknow.net.translate.IKTranslateManager;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.LogUtil;
import com.iknow.util.StringUtil;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SelectScreenManager
{
  public static final Pattern ENGSTR_PATTERN = Pattern.compile("^[A-Za-z]+$");
  private View.OnClickListener SearchWordClickListener;
  private Context mContext;
  private Word mCurrentWordInfo;
  private LayoutInflater mInflater;
  private boolean mIsGetInfoSuccess;
  private boolean mIsGetting;
  private View mParentView;
  private ProgressBar mProgressBar;
  private Button mSearchButton;
  private WordTask mTask;
  protected TaskListener mTaskListener;
  private TextView mWordDes;
  private TextView mWordKey = null;
  private RelativeLayout mWordLayout;
  private RelativeLayout mWordTouchLayout;
  private PopupWindow mWordWindow;
  private PopupWindow menuPopupWindow;
  protected RelativeLayout pagelist_bottom;
  private View.OnTouchListener selectTouchListener;
  private String text_notfound;
  protected TranslatePanel translatePanel;

  public SelectScreenManager(Context paramContext, View paramView, LayoutInflater paramLayoutInflater)
  {
    this.mWordDes = null;
    this.mWordLayout = null;
    this.mProgressBar = null;
    this.text_notfound = "未找到翻译内容";
    this.translatePanel = null;
    this.pagelist_bottom = null;
    this.selectTouchListener = new View.OnTouchListener(this)
    {
      public boolean onTouch(, MotionEvent paramMotionEvent)
      {
        if ((SelectScreenManager.access$0(this.this$0) != null) && (SelectScreenManager.access$0(this.this$0).isShowing()))
          SelectScreenManager.access$0(this.this$0).dismiss();
        if (paramView instanceof TextView)
        {
          Layout localLayout = ((TextView)paramView).getLayout();
          if ((localLayout != null) && (paramMotionEvent != null))
          {
            int i = localLayout.getOffsetForHorizontal(localLayout.getLineForVertical((int)paramMotionEvent.getY()), (int)paramMotionEvent.getX());
            SelectScreenManager.access$1(this.this$0, (TextView)paramView, i);
          }
          if ((IKnow.mSystemConfig.getBoolean("switch_bn_auto")) && (SelectScreenManager.access$2(this.this$0) != null))
            SelectScreenManager.access$3(this.this$0, SelectScreenManager.access$2(this.this$0).getText().toString());
        }
        return false;
      }
    };
    this.SearchWordClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        SelectScreenManager.access$3(this.this$0, SelectScreenManager.access$2(this.this$0).getText().toString());
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "GetDataTask";
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        SelectScreenManager.access$6(this.this$0).setVisibility(8);
        SelectScreenManager.access$5(this.this$0).setVisibility(0);
        if (paramTaskResult != TaskResult.OK)
          Toast.makeText(SelectScreenManager.access$7(this.this$0), SelectScreenManager.access$8(this.this$0).getErrorMessage(), 0).show();
        while (true)
        {
          SelectScreenManager.access$9(this.this$0, false);
          return;
          SelectScreenManager.access$5(this.this$0).setBackgroundResource(2130837811);
        }
      }

      public void onPreExecute()
      {
        SelectScreenManager.access$4(this.this$0).setVisibility(8);
        SelectScreenManager.access$5(this.this$0).setVisibility(8);
        SelectScreenManager.access$6(this.this$0).setVisibility(0);
      }

      public void onProgressUpdate(, Object paramObject)
      {
        Word localWord = (Word)paramObject;
        SelectScreenManager.access$4(this.this$0).setVisibility(0);
        if (localWord == null)
          SelectScreenManager.access$4(this.this$0).setText(SelectScreenManager.access$10(this.this$0));
        while (true)
        {
          return;
          SelectScreenManager.access$2(this.this$0).setTag(localWord.getKey());
          if (!(StringUtil.isEmpty(localWord.getPron())))
            SelectScreenManager.access$2(this.this$0).setText(localWord.getKey() + " [" + localWord.getPron() + "]");
          while (true)
          {
            while (true)
            {
              if (!(StringUtil.isEmpty(localWord.getDef())))
                break label177;
              SelectScreenManager.access$4(this.this$0).setText(SelectScreenManager.access$10(this.this$0));
            }
            SelectScreenManager.access$2(this.this$0).setText(SelectScreenManager.access$2(this.this$0).getText() + "[未找到音标]");
          }
          label177: SelectScreenManager.access$4(this.this$0).setText(localWord.getDef());
        }
      }
    };
    this.mContext = paramContext;
    this.mParentView = paramView;
    this.mInflater = paramLayoutInflater;
  }

  private void cancelView()
  {
    this.mCurrentWordInfo = null;
    this.mIsGetInfoSuccess = false;
    this.mIsGetting = false;
    this.mWordDes.setText(null);
    this.mProgressBar.setVisibility(8);
  }

  private void getWordInfo(String paramString)
  {
    if (StringUtil.isEmpty(paramString));
    while (true)
    {
      while (true)
      {
        label105: 
        do
          while (true)
          {
            do
            {
              return;
              if (this.mIsGetInfoSuccess)
                break label105;
            }
            while ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
            this.mIsGetting = true;
            this.mTask = new WordTask(this, null);
            this.mTask.setListener(this.mTaskListener);
            TaskParams localTaskParams = new TaskParams();
            localTaskParams.put("word", paramString);
            WordTask localWordTask = this.mTask;
            TaskParams[] arrayOfTaskParams = new TaskParams[1];
            arrayOfTaskParams[0] = localTaskParams;
            localWordTask.execute(arrayOfTaskParams);
          }
        while (this.mCurrentWordInfo == null);
        if ((!(StringUtil.isEmpty(this.mCurrentWordInfo.getPron()))) || (this.mCurrentWordInfo.getSentList().size() != 0) || (this.mCurrentWordInfo.getSuggList().size() != 0))
          break;
        Toast.makeText(this.mContext, this.text_notfound, 0).show();
      }
      if (this.translatePanel == null)
        this.translatePanel = new TranslatePanel(this.mContext);
      this.translatePanel.setDisplay(this.mCurrentWordInfo);
    }
  }

  private void initWordWindow()
  {
    this.mWordLayout = ((RelativeLayout)this.mInflater.inflate(2130903137, null));
    this.mWordTouchLayout = ((RelativeLayout)this.mWordLayout.findViewById(2131296592));
    this.mWordKey = ((TextView)this.mWordLayout.findViewById(2131296593));
    this.mWordDes = ((TextView)this.mWordLayout.findViewById(2131296594));
    this.mProgressBar = ((ProgressBar)this.mWordLayout.findViewById(2131296345));
    this.mSearchButton = ((Button)this.mWordLayout.findViewById(2131296595));
    this.mSearchButton.setOnClickListener(this.SearchWordClickListener);
    this.mWordWindow = new PopupWindow(this.mWordLayout, -2, -2);
    this.mWordWindow.setBackgroundDrawable(new ColorDrawable(0));
    this.mWordWindow.setOutsideTouchable(false);
    this.mWordWindow.update();
    if (TranslatePanel.FACE_PRON == null)
      TranslatePanel.FACE_PRON = Typeface.createFromAsset(this.mContext.getAssets(), "fonts/segoeui.ttf");
    this.mWordKey.setTypeface(TranslatePanel.FACE_PRON);
    this.mWordWindow.setOnDismissListener(new PopupWindow.OnDismissListener(this)
    {
      public void onDismiss()
      {
        if ((SelectScreenManager.access$8(this.this$0) != null) && (SelectScreenManager.access$8(this.this$0).getStatus() == AsyncTask.Status.RUNNING))
          SelectScreenManager.access$8(this.this$0).cancel(true);
        SelectScreenManager.access$14(this.this$0);
      }
    });
    this.mWordTouchLayout.setOnClickListener(this.SearchWordClickListener);
  }

  private boolean isLoading()
  {
    int i;
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  private boolean isShowDes()
  {
    int i;
    if ((this.mWordWindow != null) && (this.mWordWindow.isShowing()))
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  private void showWord()
  {
    if (this.mWordWindow == null)
      initWordWindow();
    this.mWordWindow.showAtLocation(this.mParentView, 80, 0, 0);
  }

  private void showWordView(String paramString)
  {
    if (isLoading());
    while (true)
    {
      label58: String str;
      do
      {
        while (true)
        {
          do
          {
            return;
            if (!(StringUtil.isEmpty(paramString)))
              break label58;
          }
          while ((this.mWordWindow == null) || (!(this.mWordWindow.isShowing())));
          this.mWordKey.setText(null);
          this.mWordDes.setText(null);
          this.mWordWindow.dismiss();
        }
        str = paramString.replaceAll("　", "").replaceAll(" ", "");
      }
      while ((isShowDes()) && (StringUtil.equalsString(StringUtil.objToString(this.mWordKey.getText()), str)));
      showWord();
      this.mWordKey.setTag(str);
      this.mWordKey.setText(str);
      this.mWordDes.setText(null);
      this.mSearchButton.setBackgroundResource(2130837747);
      this.mIsGetInfoSuccess = false;
    }
  }

  private void textEngSelect(TextView paramTextView, int paramInt)
  {
    String str1;
    char[] arrayOfChar;
    int i;
    String str2;
    int l;
    int i1;
    if (this.mIsGetting);
    while (true)
    {
      do
      {
        return;
        str1 = paramTextView.getText().toString();
        arrayOfChar = str1.toCharArray();
        i = arrayOfChar.length;
      }
      while (i == 0);
      if (i != paramInt)
        break;
      showWordView(null);
    }
    int j = 0;
    int k = i;
    if (paramInt < 0)
      paramInt = 0;
    if (paramInt >= i)
      paramInt = i - 1;
    try
    {
      Matcher localMatcher = ENGSTR_PATTERN.matcher(String.valueOf(arrayOfChar[paramInt]));
      if (localMatcher.matches())
      {
        l = paramInt;
        if (l < 0)
        {
          i1 = paramInt;
          if (i1 < arrayOfChar.length)
            break label182;
          str2 = str1.substring(j, k);
          label182: showWordView(str2);
        }
      }
    }
    catch (Exception localException)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                  LogUtil.e(super.getClass(), localException);
                if (ENGSTR_PATTERN.matcher(String.valueOf(arrayOfChar[l])).matches())
                  break;
                j = l + 1;
              }
              --l;
            }
            if (ENGSTR_PATTERN.matcher(String.valueOf(arrayOfChar[i1])).matches())
              break;
            k = i1;
          }
          ++i1;
        }
        str2 = null;
      }
    }
  }

  public void dismissWindow()
  {
    if (this.mWordWindow != null)
      this.mWordWindow.dismiss();
    if (this.translatePanel != null)
      this.translatePanel.dismiss();
  }

  public void register()
  {
  }

  public void registerOnTouchListener(View paramView)
  {
    if (paramView == null);
    while (true)
    {
      return;
      paramView.setOnTouchListener(this.selectTouchListener);
    }
  }

  public void setMenuPopupWindow(PopupWindow paramPopupWindow)
  {
    this.menuPopupWindow = paramPopupWindow;
  }

  private class WordTask extends GenericTask
  {
    private String mErrorMsg;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        String str1 = localTaskParams.getString("word");
        SelectScreenManager.access$11(this.this$0, IKnow.mTranslateManager.translate(str1, false));
        if (SelectScreenManager.access$12(this.this$0) != null)
        {
          String str2 = IKnow.mSystemConfig.getString("account_username");
          SelectScreenManager.access$12(this.this$0).setUserId(str2);
        }
        SelectScreenManager.access$13(this.this$0, true);
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = SelectScreenManager.access$12(this.this$0);
        publishProgress(arrayOfObject);
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
      catch (HttpException localHttpException)
      {
        this.mErrorMsg = localHttpException.getMessage();
        localTaskResult = TaskResult.FAILED;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMsg;
    }
  }
}